
Partial Class Admin
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetStatus("")
        If ConfigurationManager.AppSettings("AssessmentsUrl") IsNot Nothing Then
            Me.AssessmentHyperLink.Visible = True
            Me.AssessmentSeperatorImage.Visible = True
        Else
            Me.AssessmentHyperLink.Visible = False
            Me.AssessmentSeperatorImage.Visible = False
        End If

        Try


            CheckPagePermission()
            If Not Me.IsPostBack() Then
                If UserAuthorization.HasUpdatePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.Admin) Then

                    Me.divAdminMenu.Visible = True
                    Me.divFMSAMenu.Visible = False
                    Me.divSBAdminMenu.Visible = False

                Else

                    If UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.FMSAAdmin Then

                        Me.divFMSAMenu.Visible = True
                        Me.divAdminMenu.Visible = False
                        Me.divSBAdminMenu.Visible = False
                    ElseIf UserAuthorization.CurrentUser.RoleId = Cdm.DataCore.UserSecurity.Role.RoleType.SupportBrokerAdmin Then
                        Me.divFMSAMenu.Visible = False
                        Me.divAdminMenu.Visible = False
                        Me.divSBAdminMenu.Visible = True
                    Else

                        Me.divFMSAMenu.Visible = False
                        Me.divAdminMenu.Visible = False
                        ' Response.Redirect(EntityController.ApplicationHomePage())
                    End If

                End If

            End If

            If Me.IsPostBack Then
                If ScrollValue() > 0 Then
                    Me.Page.ClientScript.RegisterStartupScript(GetType(Page), "startup", "<script type=""text/javascript"" language=""javascript""> getScroll('PageDiv'," + Request("HiddenScrollPositionValue") + "); </script>")
                End If
            End If
            Me.Page.ClientScript.RegisterOnSubmitStatement(GetType(Page), "frmsubmit", "setScrollById('PageDiv');")


        Catch ex As Exception

            Cdm.AppServices.Log.ErrorLog("Admin", "An error occurred in the Page_Load() method.", ex)

        End Try

    End Sub

    Public Sub SetStatus(ByVal message As String)
        Me.statusLabel.Text = message
    End Sub

    Private Sub CheckPagePermission()
        'For the users page, redirect if this is not the right user
        If Request.RawUrl().ToUpper().IndexOf("USERS.ASPX") > -1 Then
            If EntityController.IsSingleParticipantUser() Or _
               EntityController.IsGuestUser() Then
                Response.Redirect("~/default.aspx")
            End If
            Exit Sub
        End If

        'If this is the FMSA Tax Rate Page, make sure this is a FMSA Admin user
        If Request.RawUrl().ToUpper().IndexOf("FMSATAXRATE.ASPX") > -1 Then
            If CType(UserAuthorization.CurrentUser.RoleId, Cdm.DataCore.UserSecurity.Role.RoleType) <> Cdm.DataCore.UserSecurity.Role.RoleType.FMSAAdmin Then
                Response.Redirect("~/default.aspx")
            End If
            Exit Sub
        End If

        'If this is the temporary SB Permission page - make sure this is either a SB Admin
        'or admin user
        If Request.RawUrl().ToUpper().IndexOf("TEMPSUPPORTBROKERPERMISSION.ASPX") > -1 Then
            If CType(UserAuthorization.CurrentUser.RoleId, Cdm.DataCore.UserSecurity.Role.RoleType) = Cdm.DataCore.UserSecurity.Role.RoleType.SupportBrokerAdmin Then
                Exit Sub
            End If
        End If


        UserAuthorization.CheckPagePermission(Cdm.DataCore.UserSecurity.Permission.PermissionType.Admin, "UPDATE")
    End Sub

    Private Function ScrollValue() As Integer
        If Request("HiddenScrollPositionValue") Is Nothing Then
            Return 0
        Else
            Return Convert.ToInt32(Request("HiddenScrollPositionValue"))
        End If
    End Function

End Class

